Fix off-by-one in label/key malloc β missing NUL terminator#374
Open
aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
Open
Fix off-by-one in label/key malloc β missing NUL terminator#374aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
Conversation
readstat_copy_label() allocates strlen(label) bytes and memcpy's strlen(label) bytes β no NUL terminator. Any consumer calling strlen() on the stored label reads past the end of the allocation. readstat_label_string_value() has the same bug for string_key: it stores the key string without a NUL terminator. Both allocations now use strlen + 1 and copy strlen + 1 bytes. readstat_copy_label() changes return type to readstat_error_t to propagate allocation failure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR 3: Fix off-by-one in label/key
mallocβ missing NUL terminatorSummary
Two
malloccalls inreadstat_writer.callocatestrlen(s)bytes and thenmemcpyexactlystrlen(s)bytes β omitting the NUL terminator. Thisproduces a heap allocation with no null terminator, so any code that later
calls
strlenon the stored string reads past the end of the allocation.Both sites are in the public writer API and are exercised by every caller that
registers a value label or a string-keyed label.
Site 1:
readstat_copy_labelβ label string for value labelsVulnerable code
What goes wrong
strlen("Male")returns 4.malloc(4)returns a 4-byte buffer.memcpy(..., 4)fills all 4 bytes withM,a,l,e.There is no NUL at byte 4.
If any code later does
strlen(value_label->label), it reads into the nextheap chunk until it finds a zero byte β undefined behaviour and a potential
information leak.
The
label_lenfield is used to avoidstrlenin most internal paths, sothis is silent in the common case but breaks if the label is ever treated as a
C string (e.g. in error messages, debug output, or downstream consumers of the
readstat_value_label_tstruct).Reproduction
Fix
The function signature changes from
voidtoreadstat_error_tto propagatethe allocation failure.
Site 2:
readstat_label_string_valueβ string key for string-keyed value labelsVulnerable code
What goes wrong
Identical issue:
string_keyis allocated without room for a NUL terminator.String-keyed value labels are used in SPSS SAV files for string variables.
Fix